<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Refresh" content="70; url=fix.php" />
</head>

<?PHP 
include("connect.php");
date_default_timezone_set("Asia/Chongqing");
require_once("taobaosdk/TopSdk.php");

$dNow = date('Y-m-j G:i:s');
echo $dNow."<br>";

//
$sql = "select * from fanwe_share_goods where taoke_url  like '%click%' and taoke_url  not like '%mm_25421981_0_0%'";
$result = mysql_query( $sql );
$i = 1;
while( $result && $row = mysql_fetch_assoc( $result ))
{
	if (strstr($row["taoke_url"], "click")!="" && strstr($row["taoke_url"], "mm_25421981_0_0")=="")
	{
		//echo "--".$row["taoke_url"]."--";
		$url = $row["url"];
		$url = strstr($url, "id=");
		$num_iid = substr($url,3);
		echo $url."<br>";
		
		$a = GetItemInfo($num_iid);
		$json = json_decode($a);
		if ($json->code == 1 && isset($json->click_url))
		{
			$click_url=$json->click_url;
//			$commission=$json->commission;
//			$commission_rate=$json->commission_rate;
//			$price=$json->price;
			$sql = "update fanwe_share_goods set taoke_url='$click_url' where goods_id=".$row["goods_id"];
			echo $sql;
			mysql_query( $sql );
		}
	
		
		
//		mysql_query( "update goods_items set id=$i where id=".$row["id"] );
	}
}


function GetItemInfo($itemid)
{
	$c = new TopClient;
	$c->appkey = "12351945";
	$c->secretKey = "e2218a20f1bd93de0952daed50d9ad3d";
	$req = new TaobaokeItemsConvertRequest;
	$req->setFields("click_url,num_iid,commission,commission_rate,price");
	$req->setNumIids($itemid);
	$req->setPid(25421981);
	$resp2 = $c->execute($req);
	
	if ($resp2->getName() == "taobaoke_items_convert_response" && isset($resp2->taobaoke_items))
	{
		$str = "{\"code\":1";
		$str .= ", \"click_url\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->click_url."\"";
		$str .= ", \"price\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->price."\"";
		$str .= ", \"commission\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->commission."\"";
		$str .= ", \"commission_rate\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->commission_rate."\"";
		$str .= "}";
		return $str;
	}
			
	return "{\"code\":0}";
}
?>